package com.cn.shuangzi.ad.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.provider.Settings;

import com.bytedance.msdk.api.v2.GMAdConfig;
import com.bytedance.msdk.api.v2.GMAdConstant;
import com.bytedance.msdk.api.v2.GMMediationAdSdk;
import com.bytedance.msdk.api.v2.GMPangleOption;
import com.bytedance.msdk.api.v2.GMPrivacyConfig;
import com.cn.shuangzi.SZManager;
import com.cn.shuangzi.ad.ADApp;
import com.cn.shuangzi.util.SZUtil;


/**
 * 可以用一个单例来保存TTAdManager实例，在需要初始化sdk的时候调用
 */
public class GMAdManagerHolder {

    private static boolean sInit;

    private static Boolean isCanUsePhoneState;
    private static Boolean isCanUsePrivacy;
    private static Boolean isCanUseLocation;

    public static void init(Context context) {
        doInit(context);
    }


    //step1:接入网盟广告sdk的初始化操作，详情见接入文档和穿山甲平台说明
    private static void doInit(Context context) {
        SZUtil.logError("是否已初始化：" + sInit);
        if (!sInit) {
            isCanUsePhoneState = null;
            isCanUsePrivacy = null;
            isCanUseLocation = null;
            SZUtil.logError("开始初始化");
            GMMediationAdSdk.initialize(context, buildV2Config(context));
            sInit = true;
        }
    }

    public static GMAdConfig buildV2Config(Context context) {
        if (isCanUsePrivacy == null) {
            isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
        }
        return new GMAdConfig.Builder()
                .setAppId(ADApp.getInstance().getTTAppId())
                .setAppName(ADApp.getInstance().getADPlatformAppName())
                .setDebug(SZManager.getInstance().isDebugMode())
                .setPublisherDid(getAndroidId(context))
                .setOpenAdnTest(false)
//                .setConfigUserInfoForSegment(userInfo)
                .setPangleOption(new GMPangleOption.Builder()
                        .setIsPaid(false)
                        .setTitleBarTheme(GMAdConstant.TITLE_BAR_THEME_DARK)
                        .setAllowShowNotify(true)
                        .setAllowShowPageWhenScreenLock(true)
                        .setDirectDownloadNetworkType()
//                        .setIsUseTextureView(true)
                        .setNeedClearTaskReset()
//                        .setKeywords("")
                        .build())
                .setPrivacyConfig(new GMPrivacyConfig() {
                    @Override
                    public boolean isCanUsePhoneState() {
                        if (isCanUsePhoneState == null) {
                            isCanUsePhoneState = ADUtil.isCanUsePermission(Manifest.permission
                                    .READ_PHONE_STATE);
                        }
                        SZUtil.logError("GroMore是否可使用读取设备权限：" + isCanUsePhoneState);
                        return isCanUsePhoneState;
                    }

                    @Override
                    public boolean appList() {
                        if (isCanUsePrivacy == null) {
                            isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
                        }
                        SZUtil.logError("GroMore是否可读取app列表：" + isCanUsePrivacy);
                        return isCanUsePrivacy;
                    }

                    @Override
                    public boolean isCanUseLocation() {
                        if (isCanUseLocation == null) {
                            isCanUseLocation = ADUtil.isCanUsePermission(Manifest.permission
                                    .ACCESS_FINE_LOCATION);
                        }
                        SZUtil.logError("GroMore是否可使用定位权限：" + isCanUseLocation);
                        return isCanUseLocation;
                    }

                    @Override
                    public boolean isCanUseAndroidId() {
                        if (isCanUsePrivacy == null) {
                            isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
                        }
                        SZUtil.logError("GroMore是否可读取AndroidId：" + isCanUsePrivacy);
                        return isCanUsePrivacy;
                    }

                    @Override
                    public boolean isCanUseWifiState() {
                        if (isCanUsePrivacy == null) {
                            isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
                        }
                        SZUtil.logError("GroMore是否可读取wifi状态：" + isCanUsePrivacy);
                        return isCanUsePrivacy;
                    }

                    @Override
                    public boolean isCanUseMacAddress() {
                        if (isCanUsePrivacy == null) {
                            isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
                        }
                        SZUtil.logError("GroMore是否可读取mac地址：" + isCanUsePrivacy);
                        return isCanUsePrivacy;
                    }

                    @Override
                    public String getMacAddress() {
                        return super.getMacAddress();
                    }
                })
                .build();
    }

    public static String getAndroidId(Context context) {
        String androidId = null;
        try {
            if (isCanUsePrivacy == null) {
                isCanUsePrivacy = !ADUtil.isFirstDayUseAd();
            }
            if (isCanUsePrivacy) {
                androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return androidId;
    }

}
